src/isPerfectSquare.js
zzmq.dev@gmail.com[dev1]/mathkit[add-isperfectsquare-n-function-tests] / src / isPerfectSquare.js
@ 38505f3e
Download
Ladder Summary
- file Add isPerfectSquare function with validation and tests, and export it from the main entry point
/**
* Determines whether a given number is a perfect square.
*
* @param {number} n - The number to test.
* @returns {boolean} True if n is a non‑negative integer that is a perfect square, otherwise false.
*/
export function isPerfectSquare(n) {
if (typeof n !== "number" || !Number.isFinite(n)) return false;
if (!Number.isInteger(n)) return false;
if (n < 0) return false;
const root = Math.sqrt(n);
return Number.isInteger(root);
}